import axios from 'axios'
import qs from 'qs'
import { refresh } from '../api/reply'
import router from '@/router'
// 是否正在刷新的标志
let isRefreshing = false
const _axios = axios.create()
// 设置默认请求的接口地址

_axios.defaults.baseURL = process.env.VUE_APP_BASE_API



// 请求拦截器
let to 
_axios.interceptors.request.use( (config) => {  
     to = router.history.current.path
    // console.log(1);
    // if(localStorage.getItem('refreshToken')){

    //     const res = await refresh({refreshToken:localStorage.getItem('refreshToken')})
    //     console.log(res);
    // }
    // refresh({refreshToken:localStorage.getItem('refreshToken')}).then(res=>{
    //     console.log(res);
    // })
    //设置请求token
    config.headers["access-token"] = localStorage.getItem('access_token');
    //识别系统，请求头带上系统标识
    var u = navigator.userAgent;
    var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //android终端或者uc浏览器
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
    config.headers["platform"]  = 'android'
    } else if (isiOS) {
    config.headers["platform"]  = 'android'
    }
    // config.headers["access-token"] = 'Bfo9flop4VNJ_HNPFTbbA_S-LspOt9U2FoSOE5C5ruE.6TmtdixRzL4ukjMON0cY0vDnxSpM7_RAzinNEqhwkbcZQ_kEF_J3RfM2pdlO1SWpXQQAApnG35caKMMmVvc3pv_TCQc76mawcvktcSuDW4QCvTrPm7FyC7FIY9vAREL8KYhLguFmHwQpiGI6AtDt5dv0ce7bban3n2gNo-y1DH3lyG4OZdFC64L5TaysyErZArNR3V87kXK4GG7s6B54GQ.9mvgLHhjvLESIg5KKnQMi_YVaivAr_G2fyih7ivpAwiJI-tmkIaXf9GMJ3tadHj6';
    return config;
}, (error) => {
    return Promise.reject(error);
})

// 响应拦截器
_axios.interceptors.response.use(response => {
    const res = response.data
    //token过期
    if (res.code === 1003) {
           //用refresh刷新方法
            // isRefreshing = true
            // return refresh({
            //     refreshToken:localStorage.getItem("refreshToken")
            // }).then(res1=>{
            //     console.log(res1);
            //     if (res1.code == 0) {
            //         localStorage.setItem('access_token',res1.data.token)
            //         const config = response.config
            //         config.headers.Authorization = res1.data.token
            //         return _axios(config).finally(()=>{
            //         })
            //     }
    
            // })
            //跳转方法
            
            
            router.replace('login?path='+to)
       
    
}
    //1004refresh过期
    // if (code === 0) {
    //     // Message({
    //     //     type: 'success',
    //     //     message: msg
    //     // })

    // } else {
    //     // Message({
    //     //     type: 'danger',
    //     //     message: msg
    //     // })
    // }
    return response;

}, error => {
    // Message.error("网络请求发生错误，请稍后再试")
    return Promise.reject(error); // 响应错误处理
});

// 导出两个方法
export default {
    get(url, params = {}) {
        return new Promise((resolve, reject) => {
            _axios.get(url, {
                    params
                })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    post(url, params = {}) {
        return new Promise((resolve, reject) => {
            _axios.post(url, qs.stringify(params))
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
}